package net.kylewu;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * Read properties from file
 * 
 * @author Kyle Wu
 * 
 */
public class RCFFProperties {

	private String url;
	private String username;
	private String password;
	private int intervalTime;
	private boolean autoStart;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public boolean isAutoStart() {
		return autoStart;
	}

	public void setAutoStart(boolean autoStart) {
		this.autoStart = autoStart;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getIntervalTime() {
		return intervalTime;
	}

	public void setIntervalTime(int intervalTime) {
		this.intervalTime = intervalTime;
	}

	public RCFFProperties() throws DocumentException {

		SAXReader reader = new SAXReader();
		Document document = reader.read("properties.xml");
		Element root = document.getRootElement();

		this.username = root.elementText("username");
		this.password = root.elementText("password");
		this.intervalTime = Integer.valueOf(root.elementText("interval"));
		this.autoStart = Boolean.valueOf(root.elementText("autostart"));
		this.url = root.elementText("url");
	}

	public void save() throws DocumentException, IOException {
		// TODO

		SAXReader reader = new SAXReader();
		Document document = reader.read("properties.xml");
		XMLWriter writer = new XMLWriter(new FileWriter("properties.xml"));
		Element root = document.getRootElement();
		root.element("username").setText(this.username);
		root.element("password").setText(this.password);
		root.element("interval").setText(String.valueOf(this.intervalTime));
		root.element("autostart").setText(String.valueOf(this.autoStart));
		writer.write(document);
		writer.close();

	}

}
